package com.csw.dao;

import com.csw.bean.Student;
import com.csw.util.JDBCUtil;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class StudentDao {


    /**
     * 通过学号查询学生基本信息
     *
     * @param id 学号
     * @return 学生对象
     */
    public Student queryStudentById(String id) {

        Student student = null;


        //1、获取连接
        Connection con = JDBCUtil.getConnection();

        try {
            PreparedStatement stat = con.prepareStatement("select * from student where id=?");
            stat.setString(1, id);

            ResultSet resultSet = stat.executeQuery();

            //解析resultSet

            if (resultSet.next()) {

                student = new Student();

                String name = resultSet.getString("name");
                Integer age = resultSet.getInt("age");
                String gender = resultSet.getString("gender");
                String clazz = resultSet.getString("clazz");


                student.setId(id);
                student.setName(name);
                student.setAge(age);
                student.setGender(gender);
                student.setClazz(clazz);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }

        try {
            //关闭连接
            //将连接放回连接池中
            con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }


        return student;
    }

}
